package com.nchu.student_score.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nchu.student_score.mapper.*;
import com.nchu.student_score.model.*;
import com.nchu.student_score.service.SubScoreService;
import com.nchu.student_score.util.Define;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SubScoreServiceImpl implements SubScoreService {
    @Autowired
    SubScoresMapper subScoresMapper;
    @Autowired
    SubProjectMapper subProjectMapper;
    @Autowired
    SubTypeMapper subTypeMapper;
    @Autowired
    TeachClazzMapper clazzMapper;
    @Autowired
    TeachPersonalMapper personalMapper;
    @Autowired
    StudentMapper studentMapper;

    @Override
    public ResultVo<SubScores> saveSubScores(List<SubScores> scoresList) {
        ResultVo<SubScores> resultVo = new ResultVo<>();
        for(SubScores s:scoresList){
            subScoresMapper.updateByPrimaryKey(s);//逐条更新
        }
        resultVo.setStatus(0);
        return resultVo;
    }

    @Override
    public PageInfo<SubScores> getSubScoreList(SubScores input,Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(subScoresMapper.selectByProjectIdAndClazz(input));
    }

    @Override
    public ResultVo<SubProject> selectOneProjectById(SubProject subProject) {
        ResultVo<SubProject> resultVo = new ResultVo<>();
        resultVo.setObj(subProjectMapper.selectByPrimaryKey(subProject.getSubProjectId()));
        return resultVo;
    }

    @Override
    public ResultVo<SubProject> updateSubProject(SubProject subProject) {
        ResultVo<SubProject> resultVo = new ResultVo<>();

        if(subProject.getSubProjectId() == null){//新增
            subProjectMapper.insert(subProject);//新增项目
            String subProjectId = subProject.getSubProjectId();//返回的自增主键

            List<SubScores> subScoresList = new ArrayList<>();//待插入的空白子成绩单

            String teachId = subProject.getTeachId();//教学安排id
            List<String> teachIds = new ArrayList<>();//查询所需类型
            teachIds.add(teachId);
            List<Student> students = personalMapper.selectAllByTaskIds(teachIds);//查出所有被选中的"个人"
            List<TeachClazz> clazzes = clazzMapper.selectAllByTaskIds(teachIds);//查出所有被选中的"班级"
            List<Student> students1 = studentMapper.selectByClazz(clazzes);//通过被选中的班级查出学生

            for(Student s : students){//被选中的个人
                SubScores subScores = new SubScores();
                subScores.setSubProjectId(subProjectId);
                subScores.setStuId(s.getStuId());
                subScoresList.add(subScores);
            }
            for(Student s : students1){//被选中的班级中的个人
                SubScores subScores = new SubScores();
                subScores.setSubProjectId(subProjectId);
                subScores.setClazzId(s.getClazzId());
                subScores.setStuId(s.getStuId());
                subScoresList.add(subScores);
            }

            subScoresMapper.insertAll(subScoresList);//插入空白的子成绩单
            resultVo.setStatus(0);
            resultVo.setMessage("新增项目成功!");
        }
        else{//修改
            subProjectMapper.updateByPrimaryKey(subProject);
            resultVo.setStatus(0);
            resultVo.setMessage("修改项目成功!");
        }
        return resultVo;
    }

    @Override
    public PageInfo<SubProject> getSubProjectList(SubProject subProject, Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(subProjectMapper.selectByTeachId(subProject.getTeachId()));
    }

    @Override
    public ResultVo<SubType> getAllSubType() {
        ResultVo<SubType> resultVo = new ResultVo<>();
        resultVo.setList(subTypeMapper.selectAll());
        return resultVo;
    }

    @Override
    public PageInfo<SubType> getTypeList(Integer currPage) {
        if(currPage == null) currPage = 1;
        PageHelper.startPage(currPage, Define.PAGE_SIZE);
        return new PageInfo<>(subTypeMapper.selectAll());
    }

    @Override
    public ResultVo<SubType> updateType(SubType subType) {
        ResultVo<SubType> resultVo = new ResultVo<>();

        if(subType.getSubTypeId() == null){
            subTypeMapper.insert(subType);
            resultVo.setStatus(0);
            resultVo.setMessage("新增测验类型成功!");
        }
        else{
            subTypeMapper.updateByPrimaryKey(subType);
            resultVo.setStatus(0);
            resultVo.setMessage("修改测验类型成功!");
        }
        return resultVo;
    }
}
